package com.hawthorn.mq.service.sysloglogin.impl;

import com.hawthorn.entity.po.SysLogLoginPO;
import com.hawthorn.mq.repository.SysLogLoginMapper;
import com.hawthorn.mq.service.sysloglogin.SysLogLoginService;
import com.hawthorn.platform.utils.common.DateMyUtil;
import com.hawthorn.platform.utils.common.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SysLogLoginServiceImpl implements SysLogLoginService
{
  @Autowired
  private SysLogLoginMapper sysLogLoginMapper;

  @Override
  public void saveLogLogin(Map<String, Object> param)
  {
    SysLogLoginPO sysLogLoginPO = new SysLogLoginPO();

    // 登录时间
    sysLogLoginPO.setLoginTime(DateMyUtil.parseLocalDateTime(MyUtil.getValue(param.get("createTime"))));

    Map<String, Object> masData = (Map) param.get("msgData");
    Map<String, Object> userinfo = (Map) masData.get("userInfo");
    // 登录ip
    sysLogLoginPO.setLoginIp(MyUtil.getValue(masData.get("login_ip")));
    // 登录所在城市
    sysLogLoginPO.setLoginDivision(MyUtil.getValue(masData.get("cityInfo")));
    // 登录浏览器
    sysLogLoginPO.setBrowser(MyUtil.getValue(masData.get("browser")));

    // 登录账号
    String account = MyUtil.getValue(userinfo.get("account"));
    sysLogLoginPO.setUserAccount(account);
    sysLogLoginPO.setCreateBy(account);
    sysLogLoginPO.setLastUpdateBy(account);

    // 登录名称
    sysLogLoginPO.setUserName(MyUtil.getValue(userinfo.get("name")));
    // 登录用户id
    sysLogLoginPO.setUserId(userinfo.get("id") == null ? null : Long.valueOf(MyUtil.getValue(userinfo.get("id"))));
    // 登录用户所在部门id
    Long deptId = userinfo.get("deptid") == null ? null : Long.valueOf(MyUtil.getValue(userinfo.get("deptid")));
    sysLogLoginPO.setDeptId(deptId);

    sysLogLoginMapper.save(sysLogLoginPO);
  }
}
